package com.cragon.config;

import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
import org.jasig.cas.client.util.AssertionThreadLocalFilter;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;

/**
 * @Description: TODO
 * @Author: ZHOUHAILONG
 * @Date: 2020年09月14日 11:28
 */
@Configuration
@Component
public class CasCustomConfig {
    @Autowired
    SpringCasAutoconfig autoconfig;
    private static boolean casEnabled = true;

    @Bean
    public SpringCasAutoconfig getSpringCasAutoconfig() {
        return new SpringCasAutoconfig();
    }

    @Bean
    public ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> singleSignOutHttpSessionListener() {
        ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> listener = new ServletListenerRegistrationBean<>();
        listener.setEnabled(casEnabled);
        listener.setListener(new SingleSignOutHttpSessionListener());
        listener.setOrder(1);
        return listener;
    }

    /**
     * 该过滤器用于实现单点登出功能，单点退出配置，一定要放在其他filter之前
     * @return
     */
    @Bean
    public FilterRegistrationBean singleSignOutFilter() {
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new SingleSignOutFilter());
        filterRegistrationBean.setEnabled(casEnabled);

        if (autoconfig.getSignOutFilters().size() > 0) {
            filterRegistrationBean.setUrlPatterns(autoconfig.getSignOutFilters());
        } else {
            filterRegistrationBean.addUrlPatterns("/*");
        }
        filterRegistrationBean.addInitParameter("casServerUrlPrefix", autoconfig.getCasServerUrlPrefix());
        filterRegistrationBean.setOrder(3);
        return filterRegistrationBean;
    }

    /**
     * 该过滤器负责用户的认证工作
     * @return
     */
    @Bean
    public FilterRegistrationBean authenticationFilter() {
        FilterRegistrationBean<Filter> authFilter = new FilterRegistrationBean<>();
        authFilter.setFilter(new AuthenticationFilter());
        authFilter.setEnabled(casEnabled);

        if (autoconfig.getAuthFilters().size() > 0) {
            authFilter.setUrlPatterns(autoconfig.getAuthFilters());
        } else {
            authFilter.addUrlPatterns("/*");
        }

        if (autoconfig.getIgnoreFilters() != null) {
            authFilter.addInitParameter("ignorePattern", autoconfig.getIgnoreFilters());
        }

        authFilter.addInitParameter("casServerLoginUrl", autoconfig.getCasServerLoginUrl());
        authFilter.addInitParameter("serverName", autoconfig.getServerName());
        authFilter.addInitParameter("useSession", autoconfig.isUseSession() ? "true" : "false");
        authFilter.addInitParameter("redirectAfterValidation", autoconfig.isRedirectAfterValidation() ? "true" : "false");
        authFilter.setOrder(4);
        return authFilter;
    }

    /**
     * 该过滤器负责对Ticket的校验工作，使用CAS 3.0协议
     * @return
     */
    @Bean
    public FilterRegistrationBean cas30ProxyReceivingTicketValidationFilter() {
        FilterRegistrationBean<Filter> filter = new FilterRegistrationBean<>();
        filter.setFilter(new Cas30ProxyReceivingTicketValidationFilter());
        filter.setEnabled(casEnabled);

        if (autoconfig.getValidateFilters().size() > 0) {
            filter.setUrlPatterns(autoconfig.getValidateFilters());
        } else {
            filter.addUrlPatterns("/*");
        }

        filter.addInitParameter("casServerUrlPrefix", autoconfig.getCasServerUrlPrefix());
        filter.addInitParameter("serverName", autoconfig.getServerName());
        filter.setOrder(5);
        return filter;
    }

    @Bean
    public FilterRegistrationBean httpServletRequestWrapperFilter() {
        FilterRegistrationBean<Filter> filter = new FilterRegistrationBean<>();
        filter.setFilter(new HttpServletRequestWrapperFilter());
        filter.setEnabled(casEnabled);

        if (autoconfig.getRequestWrapperFilters().size() > 0) {
            filter.setUrlPatterns(autoconfig.getRequestWrapperFilters());
        } else {
            filter.addUrlPatterns("/*");
        }
        filter.setOrder(6);
        return filter;
    }

    @Bean
    public FilterRegistrationBean assertionThreadLocalFilter() {
        FilterRegistrationBean<Filter> filter = new FilterRegistrationBean<>();
        filter.setFilter(new AssertionThreadLocalFilter());
        filter.setEnabled(casEnabled);

        if (autoconfig.getAssertionFilters().size() > 0) {
            filter.setUrlPatterns(autoconfig.getAssertionFilters());
        } else {
            filter.addUrlPatterns("/*");
        }
        filter.setOrder(7);
        return filter;
    }
}
